package com.qf.hotel.dao.impl;

import com.qf.hotel.dao.FoodTypeDao;
import com.qf.hotel.pojo.FoodType;
import com.qf.hotel.utils.JdbcUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.List;

/**
 * FoodType持久层实现类
 */
public class FoodTypeDaoImpl implements FoodTypeDao {
    QueryRunner queryRunner = new QueryRunner(JdbcUtils.getDataSource());
    /**
     * 通过条件查询
     * @param foodType
     * @return
     */
    @Override
    public List<FoodType> findByCondition(FoodType foodType) throws SQLException {
        String sql = "select type_id as typeId,type_name as typeName from t_food_type where type_name like ?";
        return queryRunner.query(JdbcUtils.getConnection(),sql,new BeanListHandler<>(FoodType.class),foodType.getTypeName());
    }

    /**
     * 通过id查询
     * @param id
     * @return
     */
    @Override
    public FoodType findById(int id) throws SQLException {
        String sql = "select type_id as typeId,type_name as typeName from t_food_type where type_id=?";
        return queryRunner.query(JdbcUtils.getConnection(),sql,new BeanHandler<>(FoodType.class),id);
    }

    /**
     * 修改
     * @param foodType
     * @return
     */
    @Override
    public int update(FoodType foodType) throws SQLException {
        String sql = "update t_food_type set type_name=? where type_id=?";
        return queryRunner.update(JdbcUtils.getConnection(),sql,foodType.getTypeName(),foodType.getTypeId());
    }
}
